//
//  TravelAreaTopicViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelAreaTopicViewController.h"
#import "DataAnalysis.h"
#import "TravelTopicModel.h"
#import "TravelTopicTableView.h"
#import "TeavelTopicDetailsViewController.h"

@interface TravelAreaTopicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *loadArr;

@property (nonatomic, retain) UIView *titleView;

@end

@implementation TravelAreaTopicViewController

-(void)dealloc
{
    [_tableView release];
    [_loadArr release];
    [_titleView release];
    [_idNumber release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.loadArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self loading];
    [self createTableView];
    [self createTitle];
    
    
    
}
#pragma mark 网络请求
-(void)loading
{
    [DataAnalysis dataAnalysisGETWithUrlString:[NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%@&page=1", self.idNumber] parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
        NSArray *arr = responseObject;
        
        for (NSDictionary *dic in arr) {
            
            TravelTopicModel *model = [TravelTopicModel travelTopicModelinitWithNSDictionary:dic];
            
            [self.loadArr addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
    
}
#pragma mark 创建一个tableView
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 49 *kHeight, self.view.frame.size.width, self.view.frame.size.height - 49 * kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    
    [self.tableView registerClass:[TravelTopicTableView class] forCellReuseIdentifier:@"TravelTopicTableViewCell"];
    
    self.tableView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
}
#pragma mark 创建标题
-(void)createTitle
{
    self.view.backgroundColor = [UIColor purpleColor];
    
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,kHeight * 64)];
    self.titleView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.titleView];
    [self.titleView release];
    [_titleView release];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15 * kWidth, 25 * kHeight, 20 * kWidth, 20 * kHeight);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [self.titleView addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 130 * kWidth, self.titleView.frame.size.height / 2 - 10 * kHeight, kWidth * 260, kHeight * 30)];
    
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = [NSString stringWithFormat:@"%@专题",self.strTitle];
    titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [self.titleView addSubview:titleLabel];
    [titleLabel release];
}

//返回点击事件
-(void)backAction:(UIButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.loadArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 200 *kHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelTopicTableView *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelTopicTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.topicModel = self.loadArr[indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TeavelTopicDetailsViewController *View = [[TeavelTopicDetailsViewController alloc]init];
    
    View.idstring = [self.loadArr[indexPath.row] ID];
    
    [self.navigationController pushViewController:View animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
